<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

/**
 * 首页按钮分类模型
 * Class IndexButtonCat
 * @package App\Models\CacheModel
 */
class IndexButtonCat extends BaseCache
{
    protected $table = 'wechatadmin.system_index_button_cat';
    protected $cacheModel = 'index.index_button_cat';

    /**
     * 获取首页按钮缓存
     * @param array $map
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function getIndexButtonCat($map = [])
    {
        if (empty($map) || !isset($map['ver']) || !isset($map['app_type']))
        {
            return [];
        }

        $idKey = $map['ver'] . ':' . $map['app_type'];

        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $idKey)->getDataAndSet(function () use ($map)
        {
            return $this->getIndexButtonCatByDb($map['ver'], $map['app_type']);
        });

        return $data;
    }

    /**
     * 从数据库中获取版本按钮bid
     * @param $ver
     * @param $app_type
     * @return mixed|string
     */
    protected function getIndexButtonCatByDb($ver, $app_type = 0)
    {
        try
        {
            $data = $this->where(['ver' => $ver, 'app_type' => $app_type])->value('bid');
        }
        catch (\Exception $e)
        {
            $data = '';
        }

        return $data;
    }


}
